package com.aaa.dao.impl;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.aaa.dao.PersonInfoDao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2024/7/15 14:48
 * @description：人员信息dao实现类
 * @modified By：
 * @version: 1.0
 */
public class PersonInfoDaoImpl implements PersonInfoDao {
    /**
     * @param map
     * @return boolean
     * @create by: Teacher陈
     * @description: 使用hutool工具添加数据到personinfo表中
     * @create time: 2024/7/15 14:46
     */
    @Override
    public boolean addPersonInfo(Map map) {
        //构建一个Entity
        cn.hutool.db.Entity E = cn.hutool.db.Entity.create("personinfo");
        E.set("personname", map.get("personname"))
                .set("personareaid", map.get("personareaid"))
                .set("personage", map.get("personage"))
                .set("personaddress", map.get("personaddress"));


        try {
            int insert = Db.use().insert(E);
            if (insert > 0) {
                return true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }

    @Override
    public boolean deletePersonInfo(Map map) {
        try {
            int delete = Db.use().del(Entity.create("personinfo").set("personid", map.get("personid")));
            if (delete > 0) {
                return true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }

    @Override
    public boolean updatePersonInfo(Map map) {
        try {
            int update = Db.use().update(Entity.create("personinfo").set("personname", map.get("personname")).set("personareaid", map.get("personareaid")).set("personage", map.get("personage")).set("personaddress", map.get("personaddress")), Entity.create("personinfo").set("personid", map.get("personid")));
            if (update > 0) {
                return true;
            }
        } catch (SQLException e) {
        }
        return false;
    }

    @Override
    public List<Entity> findAreaInfo() {
        try {
            List<Entity> select = Db.use().query("select * from areainfo");
            return select;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Entity findPersonInfoById(Map map) {
        try {
            Entity selectOne = Db.use().queryOne("select * from personinfo where personid=?", map.get("personid"));
            return selectOne;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public List<Entity> findPersonInfo() {
        try {
            List<Entity> select = Db.use().query("select personinfo.personid,personinfo.personname,personinfo.personareaid,personinfo.personage,personinfo.personaddress,areainfo.areaname from personinfo join areainfo on personinfo.personareaid=areainfo.areaid");
            return select;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }


}
